package com.fxd.shopping.product.app;

import java.util.Arrays;
import java.util.Map;

import com.fxd.core.valid.group.AddGroup;
import com.fxd.core.valid.util.BindingResultUtils;
import com.fxd.core.valid.group.UpdateGroup;
import com.fxd.core.valid.group.UpdateStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fxd.model.product.BrandEntity;
import com.fxd.shopping.product.service.BrandService;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.RR;


/**
 * 品牌
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 10:30:51
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RR list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return RR.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public RR info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return RR.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@Validated(AddGroup.class) @RequestBody BrandEntity brand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtils.bindResult(bindingResult);
        }
        brandService.save(brand);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtils.bindResult(bindingResult);
        }
        brandService.updateDetail(brand);

        return RR.ok();
    }

    @RequestMapping("/update/status")
    public RR updateStatus(@Validated(UpdateStatus.class) @RequestBody BrandEntity brand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtils.bindResult(bindingResult);
        }
        brandService.updateById(brand);

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return RR.ok();
    }

}
